package com.suxin;

import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Tang
 * @classname HandlerThread
 * @description [  ]
 * @date 2021/3/4 23:20
 */
public class HandlerThread extends Handler{

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    public HandlerThread(Selector selector, SocketChannel sc) {
        super(selector, sc);
    }

    @Override
    public void run() {
        System.out.println("Handler.....");
        try {
            if (sk.isWritable()) {
                System.out.println("isWritable。。。。");
            }
            if (sk.isReadable()) {
                // 读数据被触发，数据就要读出来，，不然事件 SelectionKey 不会消失，会一直触发
                System.out.println("isReadable。。。。");
                int read = sc.read(allocate);

                executorService.execute(() -> {
                    processData(read);
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}